﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using EnvDTE80;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Rhino.Mocks;
using TddGenerator.Core.Services;
using TddGenerator.Core.Services.Impl;

namespace TddGenerator.UnitTests.Services
{
    [TestFixture]
    public class ProjectRepositoryTester
    {
        [Test]
        public void Repository_should_sort_the_project_list()
        {
            MockRepository mocks = new MockRepository();
            DTE2 dte = mocks.CreateMock<DTE2>();
            Solution solution = mocks.CreateMock<Solution>();
            Projects projects = mocks.CreateMock<Projects>();

            List<Project> list = new List<Project>();
            
            list.Add(mocks.CreateMock<Project>());
            list.Add(mocks.CreateMock<Project>());
            SetupResult.For(list[0].Name).Return("z");
            SetupResult.For(list[1].Name).Return("a");
            

            using (mocks.Record())
            {
                Expect.Call(dte.Solution).Return(solution);
                Expect.Call(solution.Projects).Return(projects);
                Expect.Call(projects.GetEnumerator()).Return(list.GetEnumerator());
            }

            using (mocks.Playback())
            {
                IProjectRepository repository = new ProjectRepository(dte);
                List<string> actualprojects = new List<string>(repository.GetProjectNames());
                Assert.That(actualprojects, Is.Not.Null);
                Assert.That(actualprojects.Count, Is.EqualTo(2));
                Assert.That(actualprojects[0],Is.EqualTo("a"));
            }

            mocks.VerifyAll();               
        }

        [Test]
        public void Repository_should_loop_through_the_projects_in_a_solution()
        {
            MockRepository mocks = new MockRepository();
            DTE2 dte = mocks.CreateMock<DTE2>();
            Solution solution = mocks.CreateMock<Solution>();
            Projects projects = mocks.CreateMock<Projects>();
        
            using (mocks.Record())
            {
                Expect.Call(dte.Solution).Return(solution);
                Expect.Call(solution.Projects).Return(projects);                
                Expect.Call(projects.GetEnumerator()).Return(new List<Project>().GetEnumerator());
            }

            using (mocks.Playback())
            {
                IProjectRepository repository = new ProjectRepository(dte);
                List<string> actualprojects = new List<string>(repository.GetProjectNames());
                Assert.That(actualprojects, Is.Not.Null);
                Assert.That(actualprojects.Count,Is.EqualTo(0));
            }

            mocks.VerifyAll();   
        }
    }
}
